<template>
  <div>
    <el-dialog  :title="change.show?'修改点位':'新增点位'" :visible="visible" @close="closeFn" width="30%">
      <el-form ref="Form"  :rules="rules" :model="value" label-position="right" label-width="20%">
        <el-form-item label="点位名称" prop="name">
          <el-input v-model="value.name"  maxlength="15" autocomplete="off" placeholder="请输入内容"  style="width:80%"></el-input>
        </el-form-item>
        <el-form-item label="所在区域"  prop="regionId">
         <el-select v-model="value.regionId" placeholder="请选择" style="width:80%">
    <el-option v-for="item in areas" :key="item.id" :label="item.name" :value="item.id">
    </el-option>
  </el-select>
        </el-form-item>
        <el-form-item label="所属商圈" prop="businessId">
              <el-select v-model="value.businessId" placeholder="请选择" style="width:80%">
          <el-option v-for="item in at" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="归属合作商" prop="ownerName">
              <el-select v-model="value.ownerName" placeholder="请选择" style="width:80%">
          <el-option v-for="item in partner" :key="item.value" :label="item.label" :value="item.name">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="addr">
              <el-select  v-model="value.addr" placeholder="请选择" style="width:80%">
          <el-option label="浙江省/杭州市/钱塘区" value="浙江省/杭州市/钱塘区">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item style="margin-left:70px;" prop="detail">
          <el-input v-model="value.detail"  maxlength="60" autocomplete="off" type="textarea" placeholder="请输入内容" :rows="3" style="width:90%"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeFn()">取 消</el-button>
        <el-button type="primary" @click.native="abst">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { area, at, partner, add, revise } from '@/api/getLevel/getLevel'
export default {
  async created () {
    const { currentPageRecords } = await area()
    this.areas = currentPageRecords
    this.at = await at()
    const reg = await partner({ pageIndex: 1, pageSize: 100000 })
    this.partner = reg.currentPageRecords
  },
  watch: {
    change: {
      deep: true,
      handler () {
        if (this.change.show) {
          this.value.id = this.change.id
          this.value.businessId = this.change.businessId
          this.value.name = this.change.name
          this.value.ownerId = this.change.ownerId
          this.value.ownerName = this.change.ownerName
          this.value.regionId = this.change.regionId
          this.value.reg = this.change.reg
          this.value.areaCode = this.change.areaCode
          // 详细地址是最后一个输入框的内容
          // 把名字修改即可
          this.value.detail = this.change.detail
          this.value.addr = this.change.addr

          // change是对象只能一个个赋值
        }
      }
    }

  },
  data () {
    return {
      areas: '',
      at: '',
      partner: '',
      value: {
        id: '',
        reg: '',
        createUserId: 1,
        name: '',
        addr: '',
        regionId: '',
        businessId: '',
        ownerName: '',
        areaCode: '110100',
        ownerId: '',
        detail: ''
      },
      rules: {
        name: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        detail: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        regionId: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        ownerName: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        addr: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        ownerId: [{ required: true, message: '请输入活动名称', trigger: 'blur' }]
      }
    }
  },
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    change: {
      type: Object,
      required: true
    }
  },
  methods: {
    closeFn () {
      this.value = {
        id: '',
        reg: '',
        createUserId: 1,
        name: '',
        addr: '',
        regionId: '',
        businessId: '',
        ownerName: '',
        areaCode: '110100',
        ownerId: '',
        detail: ''
      }
      this.$refs.Form.resetFields()
      this.$emit('update:visible', false)
    },
    async abst () {
      // 合并地址和详细地址
      this.value.addr = this.value.addr.split('/')
      this.value.addr.push(this.value.detail)
      this.value.addr = this.value.addr.join('-')
      console.log(this.value)
      if (this.change.show) {
        const res = await revise(this.value)
        console.log(res)
      } else {
        this.value.ownerId = (this.partner.filter(item => item.name === this.value.ownerName))[0].id
        await add(this.value)
      }
      this.$emit('abst')
      this.closeFn()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
